// 公交卡余额
let transportBalance = 15.80;

// 更新余额显示
function updateTransportBalance() {
  document.getElementById('transport-balance').textContent = transportBalance.toFixed(2);
}

// 显示充值对话框
function showChargeDialog() {
  document.getElementById('charge-dialog').style.display = 'block';
  document.getElementById('records-section').style.display = 'none';
  // 重置选择
  const amounts = document.querySelectorAll('.charge-amount');
  amounts.forEach(amount => amount.classList.remove('selected'));
  document.getElementById('custom-amount').style.display = 'none';
}

// 选择充值金额
let selectedAmount = 0;
function selectChargeAmount(amount) {
  selectedAmount = amount;
  const amounts = document.querySelectorAll('.charge-amount');
  amounts.forEach(amountEl => amountEl.classList.remove('selected'));
  event.target.classList.add('selected');
  
  if (amount === 0) {
    document.getElementById('custom-amount').style.display = 'block';
  } else {
    document.getElementById('custom-amount').style.display = 'none';
  }
}

// 确认充值
function confirmCharge() {
  let amount = selectedAmount;
  
  if (amount === 0) {
    amount = parseFloat(document.getElementById('charge-input').value);
    if (isNaN(amount) || amount <= 0) {
      alert('请输入有效金额');
      return;
    }
  }
  
  if (confirm(`确认充值 ${amount} 元？`)) {
    // 模拟充值过程
    transportBalance += amount;
    updateTransportBalance();
    
    // 添加充值记录
    const recordsSection = document.getElementById('records-section');
    const newRecord = document.createElement('div');
    newRecord.className = 'history-item';
    newRecord.innerHTML = `
      <span>${new Date().toLocaleString()}</span>
      <span>+${amount.toFixed(2)}元</span>
      <span>卡片充值</span>
    `;
    recordsSection.insertBefore(newRecord, recordsSection.firstChild.nextSibling);
    
    alert('充值成功！');
    document.getElementById('charge-dialog').style.display = 'none';
  }
}

// 显示交易记录
function showRecords() {
  document.getElementById('records-section').style.display = 'block';
  document.getElementById('charge-dialog').style.display = 'none';
}

// 隐藏交易记录
function hideRecords() {
  document.getElementById('records-section').style.display = 'none';
}

// 页面加载时初始化
window.onload = function() {
  updateTransportBalance();
  
  // 模拟生成乘车码（实际应用中应该是动态生成的）
  setInterval(() => {
    console.log('乘车码已刷新');
  }, 60000); // 每分钟刷新一次
};